<?php

require_once 'Zend/View/Helper/Abstract.php';

class Core_View_Helper_Controls_SelectModule extends Zend_View_Helper_Abstract
{
    public function selectModule($name, $value, $attribs = array())
    {
        $options = array();
        $modules = Core_Filesystem_Dir::getDirectories(APPLICATION_PATH . '/modules');
        if (is_array($modules)) {
            $options[] = $this->view->getTranslation('Select a module');
            $options = array_merge($options, $this->_getModuleForms());

            $attribs['multiple'] = false;
            $form = new Core_Form();
            $select = $form->createElement('select', $name, array(
                'multiOptions'  => $options,
                'value'         => $value,
                'belongsTo'     => $name,
            ));
            if (is_array($attribs)) {
                $select->setAttribs($attribs);
            }
            return $select;
        } else {
            return $this->view->getTranslation('There are no modules currently installed');
        }
    }

    protected function _getModuleForms()
    {
        $moduleForms = array();
        $modules = Core_Filesystem_Dir::getDirectories(APPLICATION_PATH . '/modules');
        if (is_array($modules)) {
            foreach ($modules as $module) {
                $pages = Core_Filesystem_File::getFilesByType(APPLICATION_PATH . '/modules/' . $module . '/views/scripts/public', 'phtml');
                if (is_array($pages)) {
                    foreach ($pages as $page) {
                        if (strpos($page, '.form.')) {
                            $page = Core_Toolbox_Regex::stripFileExtension($page);
                            $page = str_replace('.form', '', $page);
                            $moduleForms[$module . '_' . $page] = $this->view->getTranslation($module) . ' -> ' . $page;
                        }
                    }
                }
            }
            return $moduleForms;
        }
        return false;
    }
}